package cn.terys.terystransactionclient;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import cn.emagsoftware.ui.adapterview.DataHolder;
import cn.emagsoftware.ui.adapterview.GenericAdapter;
import cn.emagsoftware.ui.adapterview.ViewHolder;
import cn.emagsoftware.ui.staticmanager.StaticManager;
import cn.emagsoftware.util.MathUtilities;
import cn.terys.terystransactionclient.data.db.DBManager;

public class TransCountQueryActivity extends BaseActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        if (!WelcomeActivity.IS_APP_RUNNING)
        {
            StaticManager.restartAppWhenDalvikRecycled(this, WelcomeActivity.class);
            return;
        }
        setContentView(R.layout.transcountquery);
        setTitle(R.string.transcountquery_title);

        String batchNum = MainActivity.BATCH_NUM;

        TextView batchnum = (TextView) findViewById(R.id.batchnum);
        batchnum.setText(batchNum);

        List<Map<String, String>> data = DBManager.queryTransCount(this, batchNum);
        List<DataHolder> holders = new ArrayList<DataHolder>(data.size());
        Iterator<Map<String, String>> dataIterator = data.iterator();
        final String type = getString(R.string.transcountquery_type);
        final String items = getString(R.string.transcountquery_items);
        final String sum = getString(R.string.transcountquery_sum);
        while (dataIterator.hasNext())
        {
            holders.add(new DataHolder(dataIterator.next(), 0)
            {
                @Override
                public View onCreateView(Context arg0, int arg1, Object arg2)
                {
                    // TODO Auto-generated method stub
                    View item = getLayoutInflater().inflate(R.layout.transcountquery_list_item, null);
                    TextView type = (TextView) item.findViewById(R.id.type);
                    TextView items = (TextView) item.findViewById(R.id.items);
                    TextView sum = (TextView) item.findViewById(R.id.sum);
                    item.setTag(new ViewHolder(type, items, sum));
                    onUpdateView(arg0, arg1, item, arg2);
                    return item;
                }

                @Override
                public void onUpdateView(Context arg0, int arg1, View arg2, Object arg3)
                {
                    // TODO Auto-generated method stub
                    Map<String, String> row = (Map<String, String>) arg3;
                    View[] views = ((ViewHolder) arg2.getTag()).getParams();
                    ((TextView) views[0]).setText(type + row.get("type"));
                    ((TextView) views[1]).setText(items + row.get("items"));
                    String sumStr = MathUtilities.round2(MathUtilities.div(Long.parseLong(row.get("sum")), 100, 2), 2);
                    ((TextView) views[2]).setText(sum + sumStr);
                }

                @Override
                public void onAsyncDataExecuted(Context arg0, int arg1, View arg2, Object arg3, int arg4)
                {
                    // TODO Auto-generated method stub
                }
            });
        }
        ListView list = (ListView) findViewById(R.id.list);
        GenericAdapter adapter = new GenericAdapter(this);
        adapter.addDataHolders(holders);
        list.setAdapter(adapter);
    }

}